import { Injectable, NestMiddleware } from '@nestjs/common';
import { VisitService } from './visit/visit.service';

@Injectable()
export class VisitCounterMiddleware implements NestMiddleware {
  constructor(private readonly visitService: VisitService) {}

  async use(req: any, res: any, next: () => void) {
    // 记录访问日志
    const data = {
      ip: req.ip,
      url: req.originalUrl,
      userAgent: req.headers['user-agent'],
      origin: req.headers.origin,
      authorization: req.headers.authorization,
    };
    await this.visitService.createVisit(data);
    next();
  }
}
